package com.sz.app.oss.web.security;

import com.sz.common.base.constants.AppDomain;
import com.sz.common.base.utils.BeanUtils;
import com.sz.common.core.service.AbstractPrincipal;
import com.sz.common.core.service.role.MembershipType;
import com.sz.common.core.system.service.UserConfigService;
import org.springframework.util.StringUtils;

/**
 * Function: WebPrincipal <br>
 * Author: Charles <br>
 * Date: 2016-11-03 16:35:00
 */
public class WebPrincipal extends AbstractPrincipal {

    private static final long serialVersionUID = -2703090503914199614L;

    /**
     * @param domainId    域Id
     * @param accountId   账户Id，可能为运营账户，或者供应商，或者客户
     * @param accountName 账户登录名，可能运营账户，或者供应商，或者客户
     */
    public WebPrincipal(int domainId, int accountId, String accountName) {
        super(domainId, accountId, accountName);
    }

    @Override
    public MembershipType getMembershipType() {
        return MembershipType.USER;
    }


    @Override
    public boolean isRoot() {
        return (this.domainId == AppDomain.OPERATION && this.accountId == 1);
    }

    @Override
    public boolean isAdmin() {
        return isRoot();
    }

    @Override
    public String getPreferredLanguage() {
        if (!StringUtils.hasLength(this.preferredLanguage)) {
            UserConfigService userConfigService = BeanUtils.getBean(UserConfigService.class);
            String language = userConfigService.getUserPreferredLanguage(this.getAccountId());
            this.preferredLanguage = language;
            principalUpdated();
        }
        return this.preferredLanguage;
    }
}
